package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.entity.Category;
import net.zjitc.entity.Workplace;
import net.zjitc.exception.WorkPlaceException;
import net.zjitc.mapper.WorkplaceMapper;
import net.zjitc.service.IWorkplaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-17 13:22:12
 * @describe 服务实现类
 */
@Service
@Transactional
public class WorkplaceServiceImpl extends ServiceImpl<WorkplaceMapper, Workplace> implements IWorkplaceService {

    @Autowired
    private WorkplaceMapper workplaceMapper;

    @Override
    public List<Workplace> getListWorkplace(Integer type) throws WorkPlaceException {
        if (type == null) {
            type = 3;
        }
        WorkPlaceLevel level = WorkPlaceLevel.getWorkPlaceLevelEnum(type);
        if (level == null) {
            throw new WorkPlaceException("分类级别参数错误");
        }
        switch (level) {
            case LEVEL_ONE:
                return getOneLeveList();
            case LEVEL_TWO:
                return getTwoLeveList();
            case LEVEL_THREE:
                return getThreeLeveList();
            default:
                throw new WorkPlaceException("分类级别参数错误");
        }
    }
    private List<Workplace> getThreeLeveList() {
        List<Workplace> onelist=getListByLevel(WorkPlaceLevel.LEVEL_ONE.LEVEL-1);
        List<Workplace> twolist=getListByLevel(WorkPlaceLevel.LEVEL_TWO.LEVEL-1);
        List<Workplace> threelist=getListByLevel(WorkPlaceLevel.LEVEL_THREE.LEVEL-1);
        nodeWorkplaceToParent(twolist,threelist);
        nodeWorkplaceToParent(onelist,twolist);
        return onelist;
    }
    private List<Workplace> getTwoLeveList() {
        List<Workplace> onelist=getListByLevel(WorkplaceServiceImpl.WorkPlaceLevel.LEVEL_ONE.LEVEL-1);
        List<Workplace> twolist=getListByLevel(WorkplaceServiceImpl.WorkPlaceLevel.LEVEL_TWO.LEVEL-1);
        nodeWorkplaceToParent(onelist, twolist);
        return onelist;
    }
    
    private List<Workplace> getOneLeveList() {
        return getListByLevel(WorkPlaceLevel.LEVEL_ONE.LEVEL-1);
    }

    @Override
    public Page<Workplace> getPageWorkplace(Integer type, Integer pagenum, Integer pagesize) throws WorkPlaceException{
        if (type == null) {
            type = 3;
        }
        WorkPlaceLevel level = WorkPlaceLevel.getWorkPlaceLevelEnum(type);
        if (level == null) {
            throw new WorkPlaceException("分类级别参数错误");
        }
        switch (level) {
            case LEVEL_ONE:
                return getOneLevePage(pagenum, pagesize);
            case LEVEL_TWO:
                return getTwoLevePage(pagenum, pagesize);
            case LEVEL_THREE:
                return getThreeLevePage(pagenum, pagesize);
        }
        return null;
    }

    @Override
    public Workplace addLevel(Workplace workplace) {
        Integer level=workplace.getLevel();
        if (level==null){
            workplace.setPid(0);
        }else {
            workplace.setPid(workplace.getPid());
        }
        int insert = workplaceMapper.insert(workplace);
        return workplace;
    }

    @Override
    public Workplace findById(Integer id) {
        QueryWrapper<Workplace> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,placename,pid,level,deleted");
        queryWrapper.eq("id",id);
        Workplace category = workplaceMapper.selectOne(queryWrapper);
        return category;
    }

    @Override
    public Workplace updateByid(Workplace workplace) {
        QueryWrapper<Workplace> wrapper=new QueryWrapper<>();
        wrapper.eq("id",workplace.getId());
        int update = workplaceMapper.update(workplace, wrapper);
        return workplace;
    }

    @Override
    public Integer deleteById(Integer id) {
        Workplace workplace = workplaceMapper.selectById(id);
        int i=0 ;
        if (workplace.getLevel()==1){
            QueryWrapper<Workplace> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",id);
            i = workplaceMapper.delete(wrapper);
        }
        else if (workplace.getLevel()==0){
            QueryWrapper<Workplace> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",id);
            List<Workplace> workplaces = workplaceMapper.selectList(wrapper);
            for (Workplace workplace1: workplaces) {
                QueryWrapper<Workplace> wrapper1=new QueryWrapper<>();
                wrapper1.eq("pid",workplace1.getId());
                workplaceMapper.delete(wrapper1);
                workplaceMapper.deleteById(workplace1.getId());
            }
        }
        i = workplaceMapper.deleteById(id);
        return i;
    }

    private Page<Workplace> getThreeLevePage(Integer pagenum,Integer pagesize) {
        Page<Workplace> onelist=getPageByLevel(WorkPlaceLevel.LEVEL_ONE.LEVEL-1,pagenum,pagesize);
        List<Workplace> twolist=getListByLevel(WorkPlaceLevel.LEVEL_TWO.LEVEL-1);
        List<Workplace> threelist=getListByLevel(WorkPlaceLevel.LEVEL_THREE.LEVEL-1);
        nodeWorkplaceToParent(twolist,threelist);
        nodeWorkplaceToParent(onelist.getRecords(),twolist);
        return onelist;
    }

    private Page<Workplace> getTwoLevePage(Integer pagenum, Integer pagesize) {
        Page<Workplace> onelist=getPageByLevel(WorkPlaceLevel.LEVEL_ONE.LEVEL-1,pagenum,pagesize);
        List<Workplace> twolist=getListByLevel(WorkPlaceLevel.LEVEL_TWO.LEVEL-1);
        nodeWorkplaceToParent(onelist.getRecords(), twolist);
        return onelist;
    }

    private void nodeWorkplaceToParent(List<Workplace> parentlsit, List<Workplace> nodelist) {
        for (Workplace parent: parentlsit) {
            List<Workplace> temp=new ArrayList<>();
            for (Workplace node: nodelist) {
                if(parent.getId().intValue()==node.getPid().intValue()){
                    temp.add(node);
                }
            }
            parent.setChildren(temp);
        }
    }

    private Page<Workplace> getOneLevePage(Integer pagenum, Integer pagesize) {
        return getPageByLevel(WorkPlaceLevel.LEVEL_ONE.LEVEL - 1, pagenum, pagesize);


    }

    private Page<Workplace> getPageByLevel(int level, Integer pagenum, Integer pagesize) {
        QueryWrapper<Workplace> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,placename,pid,level");
        queryWrapper.eq("level",level);
        Page<Workplace> page=new Page<>(pagenum,pagesize);
        return workplaceMapper.selectPage(page,queryWrapper);

    }
    private List<Workplace> getListByLevel(Integer level){
        QueryWrapper<Workplace> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,placename,pid,level");
        queryWrapper.eq("level",level);
        return workplaceMapper.selectList(queryWrapper);
    }

    public static enum WorkPlaceLevel {
        LEVEL_ONE(1, "一级分类"),
        LEVEL_TWO(2, "二级分类"),
        LEVEL_THREE(3, "三级分类"),
        ;

        WorkPlaceLevel(Integer LEVEL, String MESSAGE) {
            this.LEVEL = LEVEL;
            this.MESSAGE = MESSAGE;

        }

        private final Integer LEVEL;
        private final String MESSAGE;

        public static WorkPlaceLevel getWorkPlaceLevelEnum(Integer type) {
            for (WorkPlaceLevel level : WorkPlaceLevel.values()) {
                if (level.LEVEL.intValue() == type.intValue()) {
                    return level;
                }
            }
            return null;
        }
    }

    
}
